<?php
/**
 * 学校编辑
 * @author xj
 *
 */
namespace Superadmins\Controller;
use Superadmins\Controller;

class SchoolController extends CommonController
{
	/**
	 * 搜索页面
	 */
	static public $School;
	public  function __construct()
	{
		
		parent::__construct();
		$this->School=D('school');
	}
	public function index() 
	{
     
     	$data = $this->School->getSchoolData();
     	$School_data = $data['school_data'];
		$show=$data['show'];   	
     	$SchoolType = D('SchoolType');
        $search_type = $SchoolType->search_type();
        $this->assign('school', $School_data);
        $this->assign('page', $show); // 赋值分页输出	
        $this->assign('search', $search_type);
		$this->assign('school', $School_data);       
     	$this->display();
 	}
 		/**
 		 * 新增或编辑页面
 		 */
 		public function editor() {

        //编辑
        if (I('get.school_id')) 
        {
    		
         	
			$data=$this->School->getSchoolInfo(I('get.school_id'));
			foreach ($data['info'] as $k=>$v)
			{
				$this->assign($k,$v);
			} 
            $this->assign('address', $data['SchoolAdderss_data']);
            $this->assign('type', $data['type']);
            $this->assign('school', $data['school_data']);
            $this->assign('condition', 1);
        } 
        else 
        {        	//新增
        	$this->assign('condition', 0);
        }
        $SchoolType = D('SchoolType');
        $this->assign('schoolType',$SchoolType->search_type());
        $this->display();
    }
    
    /**
     * 根据操作选择新增或修改学校数据
     */
    public function schoolAction() { //编辑学校信息
 		
            //生成缩略图并插入数据库
  		if ($this->School->addSchool($this->Image(null,$size=array(100,100)))) {
          	$this->success('编辑成功');
           }
      
    }

    /**
            * 学校LOGO上传并且进行缩略处理
            * @return 图片地址
            */
    public function Image($path,$size=array(60,60)) {

                $file_info = $this->fileUpload($path);

                if ($file_info)
                {
                   if(!$path)
                   {
                       $file_logo = $file_info['logo'];
                       $file_url = 'Uploads/' . $file_logo['savepath'] . $file_logo['savename'];
                       //$file_name = $file_logo['savename'];
                       $url=&$file_url;
                   }
                    else
                    {
                        $url= $file_info['alumni_pic']['savepath'] . $file_info['alumni_pic']['savename'];
                        $file_url = 'Public/'.$url;

                    }
                    $image = new \Think\Image();
                    $image->open($file_url);
                    //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                    $image->thumb($size[0], $size[1])->save($file_url);
                    return $url;
                }
                return null;
                }

    /**
     * AJAX方式编辑学校相关信息
     */
    public function editorSchoolInfo() {
        $action = I('param.action');

        if($action!='school_address'){
            $model = M($action);
            foreach (I('param.') as $k => $v) {
                if ($k != 'action')
                    $data[$k] = $v;
            }

            if(!$data)
            {
                if($model->add($data));
                    $this->ajaxReturn(I('param'));
                }else{
                    if ($model->save($data))
                      $this->ajaxReturn(I('param'));
            }
        }else{
            $school_address = M('school_address');
            if(I('param.main')!=1){
                $params['school_id']=I('param.school_id');
                $this->School->SchoolTypeProcess($params['school_id'],I('param.'));
                $map['address_id']=I('param.address_id');
                $data= I('param.');
                $school_address->where($map)->save($data);
                $this->redirect('School/editor',$params);
            }else{
                
                $map['school_id']=I('param.school_id');
                $data['main']=0;
                $school_address->where($map)->save($data);
                $map['address_id']=I('param.address_id');
                $data['main']=1;
                $school_address->where($map)->save($data);
            }
        }
    }


    /**
     * AJAX方式新增学校相关信息
     */
    public function addSchoolInfo() {
        echo 1;
        $action = I('param.action');
        //除了学校地址的新增
        if($action!='school_address'){
            $model = M($action);
            foreach (I('param.') as $k => $v) {
                if ($k != 'action')
                    $data[$k] = $v;
            }
            if ($id=$model->add($data))
                $this->ajaxReturn($id);
        }else{
            $params['school_id']=I('param.school_id');
            $SchoolTypeProcess_data =$this->School->SchoolTypeProcess($params['school_id'],I('param.'));

            foreach (I('param.') as $k=>$v) {
                $SchoolTypeProcess_data[$k] = $v;
            };
            $this->School->addAddress(0,$SchoolTypeProcess_data);
            $this->redirect('School/editor',$params);
        }
    }

    /**
     * AJAX方式删除学校相关信息
     */
    public function deleteSchoolInfo() {
        $action = I('param.action');
        $model = M($action);
        foreach (I('param.') as $k => $v)
        {
            if ($k != 'action')
                $sid = $v;
        }
        if($action=='school_address')
        {

            $data = $model->field('province_relation_id,city_relation_id,county_relation_id')->where("address_id='$sid'")->find();
            $SchoolRelation = M('SchoolRelation');
            if($data)
            {
                $map['school_relation_id']=array('in',$data);
                $SchoolRelation->where($map)->delete($data);
            }

        }

        if ($model->delete($sid))
        {

            $this->ajaxReturn(I('param.'));
        }
    }

    /**
     * 点评管理
     */
    public function comment() {
        $SchoolComment = M('SchoolComment');
        $school_id = I("get.school_id");
        $map['school_id'] = $school_id;
        $map['status'] = 0;
        $count = $SchoolComment->where($map)->count(); // 查询满足要求的总记录数
        $Page = new PageController($count, 6); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $School_data = $SchoolComment->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('comment', $School_data);
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    /*
     * 查看点评功能
     *
     *   */

    public function commentselect() {

        $comment_id = I("post.comment_id");
        $map['comment_id'] = $comment_id;
        $SchoolComment = M('SchoolComment');
        $comment = $SchoolComment->where($map)->find();
//        echo $SchoolComment->_Sql();
//        print_r($comment);
        $this->ajaxReturn($comment);
    }

    /*
     * 删除点评
     * 假删除把status状态该为1
     *          */

    public function commentsdelete() {

//        $school_id = I("get.school_id");
        $comment_id = I("post.comment_id");
//        $map['school_id'] = $school_id;
        $map['comment_id'] = $comment_id;
        $data['status'] = 1;
        $SchoolComment = M('SchoolComment');

        if (($SchoolComment->where($map)->save($data)) > 0) {
            $this->ajaxReturn($comment_id);
        } else {
            $this->ajaxReturn($comment_id);
        }
    }


    public function editorcomments(){
        $school_id = I('get.school_id');
        $SchoolComment = M('SchoolComment');
        $data = I("post.");
        $SchoolComment->where()->save($data);
        $this->success("修改成功");
    }

    /**
     * 报名管理
     */
    public function apply() {
        $SchoolApply = M('SchoolApply');
        $school_id = I("get.school_id");
        $map['school_id'] = $school_id;
        $count = $SchoolApply->where($map)->count(); // 查询满足要求的总记录数
        $Page = new PageController($count, 6); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $School_data = $SchoolApply->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('apply', $School_data);
        $this->assign('school_id', $school_id);
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    Public function getOneApply(){
        $data=I("post.");
        if($data['apply_id']){
            $map['apply_id']=$data['apply_id'];
            $SchoolApply = M('SchoolApply');
            $data=$SchoolApply->where($map)->find();
        }
        $this->ajaxReturn($data);
    }

    Public function deleteApply(){
        $data=I("post.");
        if($data['apply_id']){
            $map['apply_id']=$data['apply_id'];
            $SchoolApply = M('SchoolApply');
            $flag=$SchoolApply->where($map)->delete();
        }
    }
    /*
     * 修改添加知名校友
     *      */
    Public function FamousAlumni(){
        $school_alumni=M("SchoolAlumni");
        $path='Teachers/images/alumni/';
        $pic_path=$this->Image($path);
        $alumni_id=I('post.alumni_id');
        $school_id=I('post.school_id');
        $alumni=M('SchoolAlumni');
        if($alumni_id && $school_id){
            $map['alumni_id']=$alumni_id;
            $map['school_id']=$school_id;
            $data=array(
                'alumni_content'=> I('post.alumni_content'),
                'alumni_name'   => I('post.alumni_name'),
            );
            if(!empty($pic_path)){
                $data['alumni_pic']=$pic_path;
            }
            $flag=$alumni->where($map)->save($data);
        }else if($school_id){
            $data=array(
                'alumni_content' => I('post.alumni_content'),
                'alumni_name'	 => I('post.alumni_name'),
                'school_id'	 => $school_id,
            );
            if(!empty($pic_path)){
                $data['alumni_pic']=$pic_path;
            }
            $alumni->add($data);
        }
        $this->redirect("Superadmins/school/editor",array("school_id"=>$school_id), 0);
    }
}